<?php

/*
 * @ 登录及登录处理
*/
class Admin_IndexController extends Edm_Controller_Action {

    public $adminName;
    /* 框架首页  */
    public function indexAction() {
        $this->_helper->layout->disableLayout();
    }
    
    /* 框架顶部  */
    public function topAction() {
        $this->_helper->layout->disableLayout();
        $this->view->admin_name = $this->view->identity->admin_name;
    }

    /* 框架左部 */
    public function leftAction() {
        $this->_helper->layout->disableLayout();
        /* 菜单配置 */
        $menu_mapping = array(
                '网站文章管理' => array(
                        '文章列表' => '/admin/article/',
                        '添加文章' => '/admin/article/add',
                        '分类列表' => '/admin/articlecat/',
                        '添加分类' => '/admin/articlecat/add'
                ),
                '管理员管理' => array(
                        '管理员列表' => '/admin/adminuser/',
                        '添加管理员' => '/admin/adminuser/add'
                ),
                '用户管理' => array(
                        '用户列表' => '/admin/users/'
                ),
                '其他管理' => array(
                        '后台首页' => '/admin/index/right'
                )
        );
        $this->view->menu_mapping = $menu_mapping;
    }

    /* 框架右部 */
    public function rightAction() {
        $this->view->ur_here = '信息统计';
        $sysInfo['os'] = PHP_OS;
        $sysInfo['ip'] = $this->common->getIp();
        $sysInfo['web_server'] = $this->_request->getServer('SERVER_SOFTWARE');
        $sysInfo['timezone'] = date_default_timezone_get();
        $sysInfo['socket'] = function_exists('fsockopen') ? '是' : '否';
        $sysInfo['mysql_version'] = $this->db->getServerVersion();
        $sysInfo['php_version'] = PHP_VERSION;
        $sysInfo['max_filesize'] = ini_get('upload_max_filesize');
        $this->view->sysInfo = $sysInfo;
        $countInfo['user'] = $this->_users->userCount();
        $countInfo['article'] = $this->_article->articleCount();
        $countInfo['cat'] = $this->_articlecat->catCount();
        $this->view->countInfo = $countInfo;
    }

    /* 登录 */

    public function loginAction() {
        $filter = new Zend_Filter_HtmlEntities();
        $act = $filter->filter($this->_request->getPost('act'));
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity() && isset($this->view->identity->admin_id)) {
            $this->_redirect('/admin/index');            
        }
        

        /* 登录处理 */
        $errors = array();
        if ($this->_request->isPost()) {
            $auth_num = new Zend_Session_Namespace('Auth_Code');
            $data = array(
                'username' => $filter->filter($this->_request->getPost('username')),
                'password' => $filter->filter($this->_request->getPost('password')),
                'authnum' => $filter->filter($this->_request->getPost('auth_num')),
            );
            if ($auth_num->imagecode != $data['authnum']) {
                $res = array('status' => false, 'info' => '验证码不正确！');
                die(json_encode($res));
            }

            $adapter = new Zend_Auth_Adapter_DbTable($this->db, 'admin_user', 'admin_name', 'password', 'md5(?)');
            $adapter->setIdentity($data['username']);
            $adapter->setCredential($data['password']);
            $result = $auth->authenticate($adapter);
            if ($result->isValid()) {
                $identity = $adapter->getResultRowObject(array('admin_id', 'admin_name', 'email', 'add_time', 'role'));
                $auth->getStorage()->write($identity);
                $this->_admin->loginSuccess($identity->admin_id, $this->common->getIp());
                die(json_encode(array('status' => true)));
            } else {
                die(json_encode(array('status' => false, 'info' => '用户名或密码错误！')));
            }
        }
    }

    /* 退出 */
    public function logoutAction() {
        Zend_Auth::getInstance ()->clearIdentity ();
        $this->_redirect ('/admin/index/login');
    }

}
